package com.ptmall.app.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.GoodDetilActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/3.
 */

public class Androidtojs  extends Object  {

    Context context;
    Intent intent;

    public Androidtojs(){

    }

    public Androidtojs(Context context){
        this.context = context;
    }

    @JavascriptInterface
    public void send(String data){
        Log.i("xucc",data+"-----");
        try {
            JSONObject jsonObject = new JSONObject(data);
            if(jsonObject.getString("type").equals("2")){
                 intent = new Intent(context, GoodDetilActivity.class);
                intent.putExtra("id",jsonObject.getString("productId"));
                context.startActivity(intent);
            }else {
                Map<String, String> map = new HashMap<>();
                map.put("productId", jsonObject.getString("productId"));
                map.put("num", "1");
                ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>(context) {
                    @Override
                    public void onSuccess(Object data) {
                        Toast.makeText(context,"加入购物车成功",Toast.LENGTH_LONG).show();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
